package com.qy127.controller;


import com.qy127.dao.RoleDao;
import com.qy127.entity.Role;
import com.qy127.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleDao roleDao;

    /**
     * 角色分页展示所有的角色信息
     * @return
     */
    @RequestMapping("/getAllRole")
    public Result getAllRole(){
        List<Map<String, Object>> roles = roleDao.getAllRole();
        Result result = new Result(roles.size(), roles);
        //System.out.println(result);
        return result;
    }



    @RequestMapping("/editRole")
    public Map aditRole(Role role){
        HashMap<Object, Object> map = new HashMap<>();
        //判断，如果传来的数据有rid 证明是修改 否者就是新增
        int i = 0 ;
        if (role.getRid()==null || "".equals(role.getRid())){
            i = roleDao.insertRole(role);
            if (i>0){
                map.put("code","200");
                map.put("msg","新增成功");
            }
        }else {
            i = roleDao.updateRole(role);
            if (i>0){
                map.put("code","200");
                map.put("msg","修改成功");

            }
        }
        if (i==-1){
            map.put("code","300");
            map.put("msg","操作失败");
        }
        return map;
    }

    /**
     * 查询所有的角色名称
     * @return
     */
    @RequestMapping("/getAllRoleType")
    public List<Map<String, Object>> getAllRoleType(){
        List<Map<String, Object>> allRoleType = roleDao.getAllRoleType();

        //System.out.println(allRoleType+"1111111");

        //System.out.println(allRoleType);

        return allRoleType;
    }
}
